package com.mr.qa.action.check;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.mr.qa.GlobalConfigs;

public class DailyAddSharedFileCheck implements DailyCheck {
	
	public static HashMap userSharedFileSize;
	public static String recordDateString;//yyyy-mm-dd

	public boolean check(int userId) {
		if(userSharedFileSize == null || userSharedFileSize.size() == 0)
            return true;
        Integer number = (Integer)userSharedFileSize.get(userId);
        if(number != null && 
        number >= GlobalConfigs.NUMBER_SHARED_FILE_A_USER_ONE_DAY)
                return false;
        
        return true;
	}

	public void init(int userId) {
		Date calendar = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(calendar);
        boolean ifNewRecord = false;
        if(!dateString.equals(recordDateString) || recordDateString == null)
        {
            ifNewRecord = true;
        }
        if(ifNewRecord)
        {
        	userSharedFileSize = new HashMap();
            recordDateString = dateString;
            userSharedFileSize.put(userId,0);
        }
	}

	public boolean update(int userId) {
		Date calendar = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(calendar);
        boolean ifNewRecord = false;
        if(!dateString.equals(recordDateString) || recordDateString == null)
        {
            ifNewRecord = true;
        }
        if(ifNewRecord || userSharedFileSize == null)
        {
        	userSharedFileSize = new HashMap();
            recordDateString = dateString;
            userSharedFileSize.put(userId,1);
        }
        else
        {
            Integer number = (Integer)userSharedFileSize.get(userId);
            if(number == null){
            	userSharedFileSize.put(userId,1);
            }
            else
            {
                if(number >= GlobalConfigs.NUMBER_SHARED_FILE_A_USER_ONE_DAY)
                    return false;
                else
                {
                	userSharedFileSize.put(userId,number+1);
                }
            }
        }
        return true;
	}

}
